/*
 * Copyright (c) 2009 the original author or authors
 *
 * Permission is hereby granted to use, modify, and distribute this file
 * in accordance with the terms of the license agreement accompanying it.
 */

package com.robotlegs.mvcs
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	import com.robotlegs.base.ContextEvent;
	import com.robotlegs.base.EventMap;
	import com.robotlegs.base.MediatorBase;
	import com.robotlegs.core.IEventMap;
	import com.robotlegs.core.IMediatorMap;

	/**
	 * Abstract MVCS <code>IMediator</code> implementation
	 */
	public class Mediator extends MediatorBase
	{

		[Inject]
		public var contextView : DisplayObjectContainer;

		[Inject]
		public var mediatorMap : IMediatorMap;

		/**
		 * @private
		 */
		protected var _eventDispatcher : IEventDispatcher;

		/**
		 * @private
		 */
		protected var _eventMap : IEventMap;

		public function Mediator()
		{
		}

		/**
		 * @inheritDoc
		 */
		override public function preRemove() : void
		{
			if (_eventMap)
				_eventMap.unmapListeners();
			super.preRemove();
		}

		/**
		 * @inheritDoc
		 */
		public function get eventDispatcher() : IEventDispatcher
		{
			return _eventDispatcher;
		}

		[Inject]
		/**
		 * @private
		 */
		public function set eventDispatcher(value : IEventDispatcher) : void
		{
			_eventDispatcher = value;
		}

		/**
		 * Local EventMap
		 *
		 * @return The EventMap for this Actor
		 */
		protected function get eventMap() : IEventMap
		{
			return _eventMap || (_eventMap = new EventMap(eventDispatcher));
		}

		/**
		 * 派发一个全局事件
		 * @param type
		 * @param body
		 * @return
		 *
		 */
		protected function dispatch(type : String, body : *) : Boolean
		{
			var event : ContextEvent = new ContextEvent(type, body);

			if (eventDispatcher.hasEventListener(type))
				return eventDispatcher.dispatchEvent(event);
			return false;
		}

		/**
		 * 添加组件监听事件
		 * @param evtDis
		 * @param type
		 * @param listener
		 * @param eventClass
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 *
		 */
		protected function addEventListener(evtDis : EventDispatcher, type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = true) : void
		{
			eventMap.mapListener(evtDis, type, listener, null, useCapture, priority, useWeakReference);
		}

		/**
		 * 移除组件监听事件
		 * @param evtDis
		 * @param type
		 * @param listener
		 * @param eventClass
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 *
		 */
		protected function removeEventListener(evtDis : EventDispatcher, type : String, listener : Function, useCapture : Boolean = false) : void
		{
			eventMap.unmapListener(evtDis, type, listener, null, useCapture);
		}

		/**
		 * 界面添加事件
		 * @param type
		 * @param listener
		 * @param eventClass
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 *
		 */
		protected function addViewListener(type : String, listener : Function, eventClass : Class = null, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = true) : void
		{
			eventMap.mapListener(IEventDispatcher(viewComponent), type, listener, eventClass, useCapture, priority, useWeakReference);
		}

		/**
		 * 界面移除事件
		 * @param type
		 * @param listener
		 * @param eventClass
		 * @param useCapture
		 *
		 */
		protected function removeViewListener(type : String, listener : Function, eventClass : Class = null, useCapture : Boolean = false) : void
		{
			eventMap.unmapListener(IEventDispatcher(viewComponent), type, listener, eventClass, useCapture);
		}

		/**
		 * 监听全局事件
		 * @param dispatcher
		 * @param type
		 * @param listener
		 * @param eventClass
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 *
		 */
		protected function addContextListener(type : String, listener : Function, eventClass : Class = null, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = true) : void
		{
			eventMap.mapListener(eventDispatcher, type, listener, eventClass, useCapture, priority, useWeakReference);
		}

		/**
		 * 移除全局事件
		 * @param dispatcher
		 * @param type
		 * @param listener
		 * @param eventClass
		 * @param useCapture
		 *
		 */
		protected function removeContextListener(type : String, listener : Function, eventClass : Class = null, useCapture : Boolean = false) : void
		{
			eventMap.unmapListener(eventDispatcher, type, listener, eventClass, useCapture);
		}
	}
}
